package config

import (
	"fmt"
	"k8s.io/client-go/tools/clientcmd/api"
)


// Valid comment lint rebel
func Valid(c *api.Config) (err error) {

	if c.Contexts == nil || len(c.Contexts) == 0 {
		err = fmt.Errorf("kube config contexts not exists")
		return
	}

	if c.Clusters == nil || len(c.Clusters) == 0 {
		err = fmt.Errorf("kube config clusters not exists")
		return
	}

	if c.AuthInfos == nil || len(c.AuthInfos) == 0 {
		err = fmt.Errorf("kube config users not exists")
		return
	}

	if _, err = GetCurrentCluster(c); err != nil {
		return
	}

	if _, err = GetCurrentAuthInfo(c); err != nil {
		return
	}

	return
}

// GetCurrentContext comment lint rebel
func GetCurrentContext(c *api.Config) (currentContext *api.Context, err error) {
	return GetContext(c, c.CurrentContext)
}

// GetContext comment lint rebel
func GetContext(c *api.Config, name string) (currentContext *api.Context, err error) {
	var ok bool
	currentContext, ok = c.Contexts[name]
	if !ok {
		err = fmt.Errorf("kube config current context %s not exists", name)
	}
	return
}

// HasContext comment lint rebel
func HasContext(c *api.Config, name string) (exists bool) {
	_, exists = c.Contexts[name]
	return
}

// GetCurrentCluster comment lint rebel
func GetCurrentCluster(c *api.Config) (currentCluster *api.Cluster, err error) {
	var currentContext *api.Context
	currentContext, err = GetCurrentContext(c)
	return GetCluster(c, currentContext.Cluster)
}

// GetCluster comment lint rebel
func GetCluster(c *api.Config, name string) (currentCluster *api.Cluster, err error) {
	var ok bool

	currentCluster, ok = c.Clusters[name]
	if !ok {
		err = fmt.Errorf("kube config current cluster %s not exists", name)
	}
	return
}

// GetCurrentAuthInfo comment lint rebel
func GetCurrentAuthInfo(c *api.Config) (currentAuthInfo *api.AuthInfo, err error) {
	var (
		ok             bool
		currentContext *api.Context
	)
	currentContext, err = GetCurrentContext(c)
	currentAuthInfo, ok = c.AuthInfos[currentContext.AuthInfo]
	if !ok {
		err = fmt.Errorf("kube config current user %s not exists", currentContext.AuthInfo)
	}
	return
}

// GetCurrentAuthInfoEnvByName comment lint rebel
func GetCurrentAuthInfoEnvByName(c *api.Config, name string) (value string, err error) {

	var currentAuthInfo *api.AuthInfo
	currentAuthInfo, err = GetCurrentAuthInfo(c)
	if err != nil {
		return
	}
	for _, env := range currentAuthInfo.Exec.Env {
		if env.Name == name {
			value = env.Value
			return
		}
	}
	return
}

// SetCurrentAuthInfoEnvByName comment lint rebel
func SetCurrentAuthInfoEnvByName(c *api.Config, name, value string) (err error) {
	var currentAuthInfo *api.AuthInfo
	currentAuthInfo, err = GetCurrentAuthInfo(c)
	if err != nil {
		return
	}
	for index, env := range currentAuthInfo.Exec.Env {
		if env.Name == name {
			currentAuthInfo.Exec.Env[index].Value = value
			return
		}
	}
	currentAuthInfo.Exec.Env = append(currentAuthInfo.Exec.Env, api.ExecEnvVar{
		Name:  name,
		Value: value,
	})
	return
}

// SetCurrentClusterID comment lint rebel
func SetCurrentClusterID(c *api.Config, clusterID string) (err error) {
	var currentCluster *api.Cluster
	currentCluster, err = GetCurrentCluster(c)
	if err != nil {
		return
	}

	var ianvsServer string
	ianvsServer, err = GetCurrentAuthInfoEnvByName(c, "server")
	if err != nil {
		return
	}
	err = SetCurrentAuthInfoEnvByName(c, "cluster-id", clusterID)
	if err != nil {
		return
	}
	currentCluster.InsecureSkipTLSVerify = true
	currentCluster.Server = fmt.Sprintf("%s/cluster/%s", ianvsServer, clusterID)

	return
}
